package com.gy.hsim.bservice.framework.mq;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;



/**
 * MQ消息发送
 * @author guiyi147
 *
 */
public class MQMessageSender  implements MessageSenderFactory{
	
	private static final Logger log = Logger.getLogger(MQMessageSender.class.getName());
	
	private MQParam mqParam;
	
	public MQMessageSender (MQParam mqParam) {
		this.mqParam = mqParam;
	}
	


	private Channel channel = null;
	
	
	@Override
	public void send(Object eventContent) {
		// TODO Auto-generated method stub
		try {
			send(JsonUtil.createJsonString(eventContent).getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	
	@Override
	public void send(Object eventContent, CodecFactory codecFactory) {
	
	          

		byte[] eventContentBytes = null;

		if (eventContent == null || codecFactory == null) {

			throw new IllegalArgumentException("发送到mq队列的实体对象不存在");

		} else {
			try {
				// 序列化对象
				eventContentBytes = codecFactory.serialize(eventContent);
				
				eventContentBytes = codecFactory.serialize(eventContent);
				
			} catch (IOException e) {
				log.log(Level.FINEST,e.getMessage());
				return;
			}
		}    			
		
		
		send(eventContentBytes);
	 
		
	}
	
	
	/**
	 * 消息发送 (需从配置文件中读取参数）
	 * @param exchangeName
	 * @param eventContent
	 */
	private  void send(byte[] eventContent ){
		

	  try {
//		   Channel channel;
//			channel = (Channel) MQConnection.getChannel("guest", "guest", "192.168.229.11", "5672");//配置文件读取
//	        
//		    String routerKey="tigase.message"; //需配置文件读取
//		    String queueName = "tigase.message.queue";//需配置文件读取
//		    String exchangeName = "tigase.exchange";//需配置文件读取
//		    channel.exchangeDeclare(exchangeName, "direct");
//		    channel.queueBind(queueName, exchangeName,routerKey);
//		    channel.basicPublish(exchangeName,routerKey, MessageProperties.BASIC, eventContent);

		    if (null == channel || !channel.isOpen()){
		    	channel = MQConnection.getChannel(mqParam.getExchange(),mqParam.getQueue(),mqParam.getRouterKey());
		    	
		    }
		   
		    channel.basicPublish(mqParam.getExchange(),mqParam.getRouterKey(), MessageProperties.BASIC, eventContent);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				if (null != channel && channel.isOpen()){
					channel.close();
				}
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
	}

	

	
	
	public static void main(String args[]){
		
		GYMqEntity mqEntity = new GYMqEntity("w_c_10055145455", "w_e_15555145423_2265", "消息测试！");
		
		String exchange = "tigase.message.exchange";
		String  queue = "tigase.message.queue2";
		String routerKey = "tigase.message.routerKey"; 
		
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[] { "D:\\workspace\\hsim\\hsim-bservice\\src\\main\\resources\\spring\\spring-global.xml" });
		
		MQParam mqParam = new MQParam();
		
		mqParam.setQueue(queue);
		
		mqParam.setExchange(exchange);
		
		mqParam.setRouterKey(routerKey);
		
		MQMessageSender mqSender = new MQMessageSender(mqParam);
		
		for(int i = 0;i < 200;i++) {
			mqSender.send(mqEntity);
		}
		
	}
}
